package com.hyfrogx.modules.zoom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.zoom.dao.HyZoomReserveDao;
import com.hyfrogx.modules.zoom.entity.HyZoomMeeting;
import com.hyfrogx.modules.zoom.entity.HyZoomReserve;
import com.hyfrogx.modules.zoom.service.HyZoomMeetingService;
import com.hyfrogx.modules.zoom.service.HyZoomReserveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Li9527
 * @date ：Created in 2019-11-16 13:07
 * @description：${description}
 * @modified By：
 * @version: 1.0.0
 */
@Slf4j
@Service("HyZoomReserveService")
public class HyZoomReserveServiceImpl extends ServiceImpl<HyZoomReserveDao, HyZoomReserve> implements HyZoomReserveService {

    @Autowired
    HyZoomReserveDao hyZoomReserveDao;

    @Autowired
    HyZoomMeetingService zoomMeetingService;


    @Override
    public Page selectPageByConditions(QueryExt query) {
        Map map = new HashMap();
        map.putAll(query.getCondition());
        query.setRecords(hyZoomReserveDao.selectPageByConditions(query, map));
        return query;
    }

    @Override
    public int jugeHaveMeetZoom(String useDate, Integer meetingcapacity) throws MyException {
        // 查询该日预约会议记录表该规模会议室的总数,返回list列表
        List<HyZoomReserve> listr = this.list(new QueryWrapper<HyZoomReserve>().eq("use_date", useDate).eq(
                "meeting_capacity", meetingcapacity));
        // 查询在会议室表中该规模的会议室的总数,返回list列表
        List<HyZoomMeeting> listz = zoomMeetingService.list(new QueryWrapper<HyZoomMeeting>().eq(
                "meetingCapacity", meetingcapacity).eq(
                "isVipZoom", 0).eq(
                "showStatus", 1
        ));
        // 如果长度相等,说明该日该类会议室已被预约完毕,返回-1
        if (listr.size() == listz.size()) {
            return -1;
        }
        // 在会议室总表集合中遍历预约会议室记录表
        for (int i = 0; i < listz.size(); i++) {
            for (int k = 0; k < listr.size(); k++) {
                // 如果存在于预约会议表中的会议室id,就将它移除
                if (listr.get(k).getZid().intValue() == listz.get(i).getId().intValue()) {
                    listz.remove(i);
                }
            }
        }
        // 最后剩下的集合不管长度=1或>1,都取集合第一位的对象的id
        int result = listz.get(0).getId();
        return result;
    }

    @Override
    public void reserve() {
        // 第一条件.当天订单
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(d);
        // 第二条件.未使用订单
        List<HyZoomReserve> zoomReserveList = this.list(new QueryWrapper<HyZoomReserve>().eq("use_date", today).eq(
                "status", FrogConstant.Status.FLASE));
        // 第三条件,当前时间大于等于预约毫秒祯
        for (HyZoomReserve zoomReserve : zoomReserveList) {
            if (System.currentTimeMillis() >= zoomReserve.getReserveTime()) {
                // 开启预约
                zoomReserve.setStatus(FrogConstant.Status.TRUE);
                zoomReserve.setUpdateAt(new Date());
                Boolean flag = this.updateById(zoomReserve);
                if (flag) {
                    // 查询会议室
                    HyZoomMeeting zoomMeeting = zoomMeetingService.getById(zoomReserve.getZid());
                    if (zoomMeeting != null) {
                        zoomMeeting.setReserveUid(zoomReserve.getUid());
                        zoomMeetingService.updateById(zoomMeeting);
                    } else {
                        log.error("会议预约----会议室消失。编号-----" + zoomReserve.getZid());
                    }
                }
            }
        }

    }

    @Override
    public void cancelReserv() {
        // 获取所有已预约绑定的公共会议室
        List<HyZoomMeeting> list = zoomMeetingService.list(new QueryWrapper<HyZoomMeeting>().isNotNull(false,"reserve_uid"));
        for(HyZoomMeeting meeting : list){
            meeting.setReserveUid(null);
            Boolean result = zoomMeetingService.updateById(meeting);
            if(result){
                log.info("已解除该会议室预约");
            }
        }
    }


}
